<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>任务管理</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui/css/layui.css">
    <link rel="stylesheet" href="../css/scroll-bar.css">
    <link rel="stylesheet" href="../css/sub-page.css">
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../lib/nprogress/nprogress.css">
</head>

<body>
    <div class="ok-body">
        <!--面包屑导航区域-->
        <div class="ok-body-breadcrumb">
            <span class="layui-breadcrumb">
                <a><cite>首页</cite></a>
                <a><cite>任务管理</cite></a>
                <a><cite>任务列表</cite></a>
            </span>
            <a class="layui-btn layui-btn-small" href="javascript:location.replace(location.href);" title="刷新">
                <i class="layui-icon layui-icon-refresh"></i>
            </a>
        </div>
        <!--模糊搜索区域-->
        <div class="layui-row ok-search">
            <input class="layui-input" placeholder="开始日期" autocomplete="off" id="startTime">
            <input class="layui-input" placeholder="截止日期" autocomplete="off" id="endTime">
            <div class="layui-inline">
                <input class="layui-input" name="id" id="taskname" autocomplete="off" placeholder="请输入任务名称">
            </div>
            <button class="layui-btn" data-type="reload" id="search">搜索</button>
        </div>
        <!--工具栏-->
        <!-- <okToolbar>
            <button class="layui-btn" id="add">
                <i class="layui-icon">&#xe61f;</i>新建任务
            </button>
            <span>共有数据：<i id="countNum">0</i> 条</span>
        </okToolbar> -->
        <!--数据表格-->
        <table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
              <button class="layui-btn layui-btn-sm" lay-event="add">新建任务</button>
            </div>
          </script>
    </div>
    <!--js逻辑-->
    <script src="../lib/layui/layui.js"></script>
    <script src="../lib/nprogress/nprogress.js"></script>
    <script>
        NProgress.start();
    window.onload = function () {
        NProgress.done();
    }
    layui.use(['element', 'table', 'form', 'jquery','laydate'], function () {
        var element = layui.element;
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        var laydate = layui.laydate;
        laydate.render({
            elem: '#startTime',
            type: "datetime"
        });

        laydate.render({
            elem: '#endTime',
            type: "datetime"
        });
        table.render({
            elem: '#tableId',
            url: '/task/list',
            limit: 10,
            cellMinWidth: 80,
            page: true,
            id:'folderlist',
            toolbar: '#toolbarDemo',
            where: {
                    full:<%=data%>
                    },
            cols: [[
                {field: 'taskid', title: 'ID', width: 80, sort: true},
                {field: 'taskname', title: '任务名'},
                {field: 'actions', title: '执行类别', templet: '#actionTpl'},
                {field: 'tasktags', title: '任务标签'},
                {field: 'description', title: '任务描述'},
                {field: 'createat', title: '创建时间'},
                {field: 'status', title: '状态', width: 85, templet: '#statusTpl'},
                {fixed: 'right',title: '操作', templet: '#operationTpl', align: 'center'}
            ]],
            done: function (res, curr, count) {
                //$("#countNum").text(count);
            }
        });

        var active = {
            reload: function(){
                var taskname = $('#taskname').val();
                var start=$('#startTime').val();
                var end=$('#endTime').val();
                
                //执行重载
                table.reload('folderlist', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        key:{
                            taskname: taskname,
                            start:start,
                            end:end                           
                        },
                        full: <%=data%>
                    }
                });
            }
        };

        $('#search').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        $('#taskname').on('keydown',function(e){
            if(e.keyCode=='13'){
                $('#search').click();
            }
        });

        table.on('tool(tableFilter)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'flowsetting') {
                layer.open({
                    title: '流程设置',
                    type: 2,
                    shade: false,
                    maxmin: true,
                    shade: 0.5,
                    area: ['90%', '90%'],
                    content: '/task/flowupdate?configid='+ data.configid,
                    zIndex: layer.zIndex,
                    end: function () {
                        $(".layui-laypage-btn")[0].click();
                    },
                    success:function(layero, index){
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        //console.log(body.html()) //得到iframe页的body内容
                        //console.log(body.find('#guestid').val());
                        body.find('#id').val(data.configid);
                        console.log(body.find('#id').val());
                    }
                });
            } else if (layEvent === 'baseinfo') {
                layer.open({
                    title: '基础设置',
                    type: 2,
                    shade: false,
                    maxmin: true,
                    shade: 0.5,
                    area: ['90%', '90%'],
                    content: '/task/baseupdate?taskid='+ data.taskid,
                    zIndex: layer.zIndex,
                    end: function () {
                        $(".layui-laypage-btn")[0].click();
                    },
                    success:function(layero, index){
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        //console.log(body.html()) //得到iframe页的body内容
                        //console.log(body.find('#guestid').val());
                        body.find('#id').val(data.taskid);
                        console.log(body.find('#id').val());
                    }
                });
            }else if (layEvent === 'publish') {
                layer.confirm("确认发布？", {
                    skin: 'layui-layer-lan',
                    icon: 3,
                    title: '提示',
                    anim: 6
                }, function () {
                    console.log(data);
                    $.ajax({
                    url: '/task/publish',
                    method: 'post',
                    data: {taskid:data.taskid},
                    dataType: 'JSON',
                    success: function (res) {
                            if (res.code == 1) {
                                $('#search').click();
                                layer.closeAll();  // 关闭layer
                            } else {
                                layer.msg('操作失败');
                            }
                        },
                        error: function (data) {
                            layer.msg('操作失败');
                        }
                    }) 
                });
            }else if (layEvent === 'statistics') {
                layer.open({
                    title: '任务统计--'+data.taskname,
                    type: 2,
                    shade: false,
                    maxmin: true,
                    shade: 0.5,
                    area: ['100%', '100%'],
                    content: '../statistics/task?isadmin=1',
                    zIndex: layer.zIndex,
                    end: function () {
                        $(".layui-laypage-btn")[0].click();
                    },
                    success:function(layero, index){
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        body.find('#isadmin').val(1);
                        console.log(body.find('#isadmin').val());
                        
                        body.find('#id').val(data.taskid);
                        console.log(body.find('#id').val());
                    }
                });
            } 
        });
  //头工具栏事件
  table.on('toolbar(tableFilter)', function(obj){
    switch(obj.event){
      case 'add':
      layer.open({
                title: '新建任务',
                type: 2,
                shade: false,
                maxmin: true,
                shade: 0.5,
                anim: 4,
                area: ['90%', '90%'],
                content: '/task/addtask',
                zIndex: layer.zIndex,
                // skin: 'layui-layer-molv',
                end: function () {
                    $(".layui-laypage-btn")[0].click();
                }
            });
      break;     
    };
  });

         $("#add").click(function () {
            layer.open({
                title: '新建任务',
                type: 2,
                shade: false,
                maxmin: true,
                shade: 0.5,
                anim: 4,
                area: ['90%', '90%'],
                content: '/task/addtask',
                zIndex: layer.zIndex,
                // skin: 'layui-layer-molv',
                end: function () {
                    $(".layui-laypage-btn")[0].click();
                }
            });
        });
    })
</script>
    <!--模板-->
    <script type="text/html" id="statusTpl">
        <!-- <input type="checkbox" name="status" value="{{d.userid}}" lay-filter='status_switch' lay-skin="switch" lay-text="完成|进行" {{ d.status== 1 ? 'checked' : ''}}> -->
    <a href="javascript:;" title="任务状态"  class="layui-btn layui-btn-xs">{{#  if(d.status === 0){ }}
            未开始
          {{#  } }}
          {{#  if(d.status === -1){ }}
            不可用
          {{#  } }}
          {{#  if(d.status === 1){ }}
            进行中
          {{#  } }}
          {{#  if(d.status === 2){ }}
            已完成
          {{#  } }}
        </a>
</script>

    <script type="text/html" id="actionTpl">
        <!-- <input type="checkbox" name="status" value="{{d.userid}}" lay-filter='status_switch' lay-skin="switch" lay-text="完成|进行" {{ d.status== 1 ? 'checked' : ''}}> -->
<a href="javascript:;" title="执行类别"  class="layui-btn layui-btn-xs">
        {{#  if(d.actions === 0){ }}
        数据筛选
        {{#  } }}     
       {{#  if(d.actions === 1){ }}
        单分类
        {{#  } }}
        {{#  if(d.actions === 2){ }}
        多分类
        {{#  } }}     
    </a>
</script>
    <script type="text/html" id="operationTpl">
        <a href="javascript:;" title="流程设置" lay-event="flowsetting" class="layui-btn layui-btn-xs layui-bg-blue">流程设置</a>
        <a href="javascript:;" title="基础设置" lay-event="baseinfo" class="layui-btn layui-btn-xs layui-bg-blue">基础设置</a>
        {{#  if(d.status === 0){ }}
        <a href="javascript:;" title="发布任务" lay-event="publish" class="layui-btn layui-btn-xs layui-bg-red">发布</a>
        {{#  } }}

        {{#  if(d.status === 1){ }}
        <a href="javascript:;" title="任务统计" lay-event="statistics" class="layui-btn layui-btn-xs layui-bg-blue">任务统计</a>
        {{#  } }}
        {{#  if(d.status === 2){ }}
        <a href="javascript:;" title="任务统计" lay-event="statistics" class="layui-btn layui-btn-xs layui-bg-blue">任务统计</a>
        {{#  } }}
        
         <!--<a href="javascript:;" title="删除" lay-event="del"><i class="layui-icon">&#xe640;</i></a> -->
</script>
</body>

</html>